package com.fau.cm.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallRecording;
import com.fau.cm.service.IRecordingService;
import com.fau.cm.util.Page;
import com.fau.cm.util.ParamUtil;
import com.fau.cm.util.SystemConfig;

@Service
@Transactional(readOnly = true)
public class RecordingService extends BaseService implements IRecordingService{
	
	@Override
	public Page findRecordingByPage(MedicalAdmin admin, MedicalCallRecording recording, Map<String, Object> map) {
		List<Object> objectList = new ArrayList<Object>();
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallRecording m where m.hospitalId=? ");
		StringBuilder statusSql = new StringBuilder("from MedicalCallRecording m where m.hospitalId=? ");
		objectList.add(admin.getEnterpriseId());
		
		if(StringUtils.isNotBlank(recording.getCallerNum())){
			countSql.append("and m.callerNum like ? ");
			statusSql.append("and m.callerNum like ? ");
			objectList.add("%"+recording.getCallerNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getCalledNum())){
			countSql.append("and m.calledNum like ? ");
			statusSql.append("and m.calledNum like ? ");
			objectList.add("%"+recording.getCalledNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getSeat())){
			countSql.append("and m.seat = ? ");
			statusSql.append("and m.seat = ? ");
			objectList.add(recording.getSeat());
		}
		if(recording.getStartTime() != null){
			countSql.append("and m.callTime >= ? ");
			statusSql.append("and m.callTime >= ? ");
			objectList.add(recording.getStartTime());
		}
		if(recording.getEndTime() != null){
			countSql.append("and m.callTime <= ? ");
			statusSql.append("and m.callTime <= ? ");
			objectList.add(recording.getEndTime());
		}
		if(recording.getTalkTimeLess() != null ){
			countSql.append("and m.talkTime < ? ");
			statusSql.append("and m.talkTime < ? ");
			objectList.add(recording.getTalkTimeLess());
		}
		if(recording.getTalkTimeMore() != null ){
			countSql.append("and m.talkTime > ? ");
			statusSql.append("and m.talkTime > ? ");
			objectList.add(recording.getTalkTimeMore());
		}
		if(StringUtils.isNotBlank(recording.getIsOpen())) {
			countSql.append("and m.isOpen = ? ");
			statusSql.append("and m.isOpen = ? ");
			objectList.add(recording.getIsOpen());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by m." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by m.callTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalCallRecording> list = new ArrayList<MedicalCallRecording>();
		for(Object obj : page.getResult()) {
			MedicalCallRecording bean = (MedicalCallRecording) obj;
			if(StringUtils.isNotBlank(bean.getSeat())) {
				MedicalAdmin seat = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getSeat());
				if(null != seat){
					bean.setSeatName(seat.getName());
					list.add(bean);
				}
			}
		}
		page.setResult(list);
		return page;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Workbook exportRecording(MedicalAdmin admin,MedicalCallRecording recording) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallRecording m where m.hospitalId=? ");
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		
		if(StringUtils.isNotBlank(recording.getCallerNum())){
			statusSql.append("and m.callerNum like ? ");
			objectList.add("%"+recording.getCallerNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getCalledNum())){
			statusSql.append("and m.calledNum like ? ");
			objectList.add("%"+recording.getCalledNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getSeat())){
			statusSql.append("and m.seat = ? ");
			objectList.add(recording.getSeat());
		}
		if(recording.getStartTime() != null){
			statusSql.append("and m.callTime >= ? ");
			objectList.add(recording.getStartTime());
		}
		if(recording.getEndTime() != null){
			statusSql.append("and m.callTime <= ? ");
			objectList.add(recording.getEndTime());
		}
		if(recording.getTalkTimeLess() != null ){
			statusSql.append("and m.talkTime < ? ");
			objectList.add(recording.getTalkTimeLess());
		}
		if(recording.getTalkTimeMore() != null ){
			statusSql.append("and m.talkTime > ? ");
			objectList.add(recording.getTalkTimeMore());
		}
		List<MedicalCallRecording> list = this.iBaseDao.getBySql(statusSql.toString(), null, objectList.toArray());
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_RECORDING);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		// 遍历
		int size = list.size();
		for (int i = 0;i<size;i++) {
			Row currentRow = sheet.createRow(i+1);
			if(StringUtils.isNotBlank(list.get(i).getCallerNum())){
				currentRow.createCell(0).setCellValue(list.get(i).getCallerNum());
			}
			if(StringUtils.isNotBlank(list.get(i).getCalledNum())){
				currentRow.createCell(1).setCellValue(list.get(i).getCalledNum());
			}
			if(StringUtils.isNotBlank(list.get(i).getSeat())){
				MedicalAdmin adMedicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, list.get(i).getSeat());
				currentRow.createCell(2).setCellValue(adMedicalAdmin.getName());
			}
			if(list.get(i).getCallTime() != null){
				currentRow.createCell(3).setCellValue(list.get(i).getCallTime());
			}
			if(list.get(i).getTalkTime() != null){
				currentRow.createCell(4).setCellValue(list.get(i).getTalkTime());
			}
			if(StringUtils.isNotBlank(list.get(i).getIsOpen())){
				if("0".equals(list.get(i).getIsOpen())){
					currentRow.createCell(5).setCellValue("不公开");
				}else if("1".equals(list.get(i).getIsOpen())){
					currentRow.createCell(5).setCellValue("公开");
				}
			}
		}
		return workbook;
	}
	
	@Override
	public Workbook currentExportRecording(MedicalAdmin admin, String ids) throws Exception {
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_RECORDING);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		Sheet sheet = workbook.getSheetAt(0);
		is.close();
		if(StringUtils.isNotBlank(ids)){
			String [] ordersIds =  ids.split(",");
			for(int j=0;j<ordersIds.length;j++){
				// 获得记录
				MedicalCallRecording recording = (MedicalCallRecording)this.iBaseDao.getObject(MedicalCallRecording.class, ordersIds[j]);
				Row currentRow = sheet.createRow(j+1);
				if(StringUtils.isNotBlank(recording.getCallerNum())){
					currentRow.createCell(0).setCellValue(recording.getCallerNum());
				}
				if(StringUtils.isNotBlank(recording.getCalledNum())){
					currentRow.createCell(1).setCellValue(recording.getCalledNum());
				}
				if(StringUtils.isNotBlank(recording.getSeat())){
					MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, recording.getSeat());
					currentRow.createCell(2).setCellValue(medicalAdmin.getName());
				}
				if(recording.getCallTime() != null){
					currentRow.createCell(3).setCellValue(recording.getCallTime());
				}
				if(recording.getTalkTime() != null){
					currentRow.createCell(4).setCellValue(recording.getTalkTime());
				}
				if(StringUtils.isNotBlank(recording.getIsOpen())){
					if("0".equals(recording.getIsOpen())){
						currentRow.createCell(5).setCellValue("不公开");
					}else if("1".equals(recording.getIsOpen())){
						currentRow.createCell(5).setCellValue("公开");
					}
				}
			}
		}
		return workbook;
	}

	@Override
	public Page findCenterRecordingByPage(MedicalAdmin admin, MedicalCallRecording recording, Map<String, Object> map) {
		List<Object> objectList = new ArrayList<Object>();
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallRecording m where 1=1 and m.hospitalId=? ");
		StringBuilder statusSql = new StringBuilder("from MedicalCallRecording m where 1=1 and m.hospitalId=? ");
		objectList.add(admin.getEnterpriseId());
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			countSql.append("and m.centerId=? ");
			statusSql.append("and m.centerId=? ");
			objectList.add(admin.getGroup().getPlatform().getId());
		}
		
		if(StringUtils.isNotBlank(recording.getCallerNum())){
			countSql.append("and m.callerNum like ? ");
			statusSql.append("and m.callerNum like ? ");
			objectList.add("%"+recording.getCallerNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getCalledNum())){
			countSql.append("and m.calledNum like ? ");
			statusSql.append("and m.calledNum like ? ");
			objectList.add("%"+recording.getCalledNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getSeat())){
			countSql.append("and m.seat = ? ");
			statusSql.append("and m.seat = ? ");
			objectList.add(recording.getSeat());
		}
		if(recording.getStartTime() != null){
			countSql.append("and m.callTime >= ? ");
			statusSql.append("and m.callTime >= ? ");
			objectList.add(recording.getStartTime());
		}
		if(recording.getEndTime() != null){
			countSql.append("and m.callTime <= ? ");
			statusSql.append("and m.callTime <= ? ");
			objectList.add(recording.getEndTime());
		}
		if(recording.getTalkTimeLess() != null ){
			countSql.append("and m.talkTime < ? ");
			statusSql.append("and m.talkTime < ? ");
			objectList.add(recording.getTalkTimeLess());
		}
		if(recording.getTalkTimeMore() != null ){
			countSql.append("and m.talkTime > ? ");
			statusSql.append("and m.talkTime > ? ");
			objectList.add(recording.getTalkTimeMore());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by m." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by m.callTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalCallRecording> list = new ArrayList<MedicalCallRecording>();
		for(Object obj : page.getResult()) {
			MedicalCallRecording bean = (MedicalCallRecording) obj;
			if(StringUtils.isNotBlank(bean.getSeat())) {
				MedicalAdmin seat = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getSeat());
				if(null != seat){
					bean.setSeatName(seat.getName());
					list.add(bean);
				}
			}
		}
		page.setResult(list);
		return page;
	}

	@Override
	public Page findGroupRecordingByPage(MedicalAdmin admin, MedicalCallRecording recording, Map<String, Object> map) {
		List<Object> objectList = new ArrayList<Object>();
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallRecording m where m.hospitalId=? ");
		StringBuilder statusSql = new StringBuilder("from MedicalCallRecording m where m.hospitalId=? ");
		objectList.add(admin.getEnterpriseId());
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			countSql.append("and m.groupId=? ");
			statusSql.append("and m.groupId=? ");
			objectList.add(admin.getGroup().getId());
		}
		
		if(StringUtils.isNotBlank(recording.getCallerNum())){
			countSql.append("and m.callerNum like ? ");
			statusSql.append("and m.callerNum like ? ");
			objectList.add("%"+recording.getCallerNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getCalledNum())){
			countSql.append("and m.calledNum like ? ");
			statusSql.append("and m.calledNum like ? ");
			objectList.add("%"+recording.getCalledNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getSeat())){
			countSql.append("and m.seat = ? ");
			statusSql.append("and m.seat = ? ");
			objectList.add(recording.getSeat());
		}
		if(recording.getStartTime() != null){
			countSql.append("and m.callTime >= ? ");
			statusSql.append("and m.callTime >= ? ");
			objectList.add(recording.getStartTime());
		}
		if(recording.getEndTime() != null){
			countSql.append("and m.callTime <= ? ");
			statusSql.append("and m.callTime <= ? ");
			objectList.add(recording.getEndTime());
		}
		if(recording.getTalkTimeLess() != null ){
			countSql.append("and m.talkTime < ? ");
			statusSql.append("and m.talkTime < ? ");
			objectList.add(recording.getTalkTimeLess());
		}
		if(recording.getTalkTimeMore() != null ){
			countSql.append("and m.talkTime > ? ");
			statusSql.append("and m.talkTime > ? ");
			objectList.add(recording.getTalkTimeMore());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by m." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by m.callTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalCallRecording> list = new ArrayList<MedicalCallRecording>();
		for(Object obj : page.getResult()) {
			MedicalCallRecording bean = (MedicalCallRecording) obj;
			if(StringUtils.isNotBlank(bean.getSeat())) {
				MedicalAdmin seat = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getSeat());
				if(null != seat){
					bean.setSeatName(seat.getName());
					list.add(bean);
				}
			}
		}
		page.setResult(list);
		return page;
	}

	@Override
	public Page findMyRecordingByPage(MedicalAdmin admin, MedicalCallRecording recording, Map<String, Object> map) {
		List<Object> objectList = new ArrayList<Object>();
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallRecording m where 1=1 and m.userId=? and m.hospitalId=? ");
		StringBuilder statusSql = new StringBuilder("from MedicalCallRecording m where 1=1 and m.userId=? and m.hospitalId=? ");
		objectList.add(admin.getId());
		objectList.add(admin.getEnterpriseId());
		if(StringUtils.isNotBlank(recording.getCallerNum())){
			countSql.append("and m.callerNum like ? ");
			statusSql.append("and m.callerNum like ? ");
			objectList.add("%"+recording.getCallerNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getCalledNum())){
			countSql.append("and m.calledNum like ? ");
			statusSql.append("and m.calledNum like ? ");
			objectList.add("%"+recording.getCalledNum()+"%");
		}
		if(recording.getStartTime() != null){
			countSql.append("and m.callTime >= ? ");
			statusSql.append("and m.callTime >= ? ");
			objectList.add(recording.getStartTime());
		}
		if(recording.getEndTime() != null){
			countSql.append("and m.callTime <= ? ");
			statusSql.append("and m.callTime <= ? ");
			objectList.add(recording.getEndTime());
		}
		if(recording.getTalkTimeLess() != null ){
			countSql.append("and m.talkTime < ? ");
			statusSql.append("and m.talkTime < ? ");
			objectList.add(recording.getTalkTimeLess());
		}
		if(recording.getTalkTimeMore() != null ){
			countSql.append("and m.talkTime > ? ");
			statusSql.append("and m.talkTime > ? ");
			objectList.add(recording.getTalkTimeMore());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by m." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by m.callTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalCallRecording> list = new ArrayList<MedicalCallRecording>();
		for(Object obj : page.getResult()) {
			MedicalCallRecording bean = (MedicalCallRecording) obj;
			if(StringUtils.isNotBlank(bean.getSeat())) {
				MedicalAdmin seat = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getSeat());
				if(null != seat){
					bean.setSeatName(seat.getName());
					list.add(bean);
				}
			}
		}
		page.setResult(list);
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Workbook exportCenterRecording(MedicalAdmin admin,MedicalCallRecording recording) throws Exception {
		StringBuilder statusSql = null;
		List<Object> objectList = new ArrayList<Object>();
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql = new StringBuilder("from MedicalCallRecording m where 1=1 and m.centerId=? and m.hospitalId=? ");
			objectList.add(admin.getGroup().getPlatform().getId());
		}else{
			statusSql = new StringBuilder("from MedicalCallRecording m where m.hospitalId=? ");
		}
		objectList.add(admin.getEnterpriseId());
		
		if(StringUtils.isNotBlank(recording.getCallerNum())){
			statusSql.append("and m.callerNum like ? ");
			objectList.add("%"+recording.getCallerNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getCalledNum())){
			statusSql.append("and m.calledNum like ? ");
			objectList.add("%"+recording.getCalledNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getSeat())){
			statusSql.append("and m.seat = ? ");
			objectList.add(recording.getSeat());
		}
		if(recording.getStartTime() != null){
			statusSql.append("and m.callTime >= ? ");
			objectList.add(recording.getStartTime());
		}
		if(recording.getEndTime() != null){
			statusSql.append("and m.callTime <= ? ");
			objectList.add(recording.getEndTime());
		}
		if(recording.getTalkTimeLess() != null ){
			statusSql.append("and m.talkTime < ? ");
			objectList.add(recording.getTalkTimeLess());
		}
		if(recording.getTalkTimeMore() != null ){
			statusSql.append("and m.talkTime > ? ");
			objectList.add(recording.getTalkTimeMore());
		}
		// 获得记录
		List<MedicalCallRecording> list = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_RECORDING);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		int size = list.size();
		for (int i = 0;i<size;i++) {
			Row currentRow = sheet.createRow(i+1);
			if(StringUtils.isNotBlank(list.get(i).getCallerNum())){
				currentRow.createCell(0).setCellValue(list.get(i).getCallerNum());
			}
			if(StringUtils.isNotBlank(list.get(i).getCalledNum())){
				currentRow.createCell(1).setCellValue(list.get(i).getCalledNum());
			}
			if(StringUtils.isNotBlank(list.get(i).getSeat())){
				MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, list.get(i).getSeat());
				if(null != medicalAdmin){
					currentRow.createCell(2).setCellValue(medicalAdmin.getName());
				}
			}
			if(list.get(i).getCallTime() != null){
				currentRow.createCell(3).setCellValue(list.get(i).getCallTime());
			}
			if(list.get(i).getTalkTime() != null){
				currentRow.createCell(4).setCellValue(list.get(i).getTalkTime());
			}
			if(StringUtils.isNotBlank(list.get(i).getIsOpen())){
				if("0".equals(list.get(i).getIsOpen())){
					currentRow.createCell(5).setCellValue("不公开");
				}else if("1".equals(list.get(i).getIsOpen())){
					currentRow.createCell(5).setCellValue("公开");
				}
			}
		}
		return workbook;
	}

	@Override
	public Workbook currentExportCenterRecording(MedicalAdmin admin, String ids) throws Exception {
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_RECORDING);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		if(StringUtils.isNotBlank(ids)){
			String [] ordersIds =  ids.split(",");
			for(int j=0;j<ordersIds.length;j++){
				// 获得记录
				MedicalCallRecording recording = (MedicalCallRecording)this.iBaseDao.getObject(MedicalCallRecording.class, ordersIds[j]);
				Row currentRow = sheet.createRow(j+1);
				if(StringUtils.isNotBlank(recording.getCallerNum())){
					currentRow.createCell(0).setCellValue(recording.getCallerNum());
				}
				if(StringUtils.isNotBlank(recording.getCalledNum())){
					currentRow.createCell(1).setCellValue(recording.getCalledNum());
				}
				if(StringUtils.isNotBlank(recording.getSeat())){
					MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, recording.getSeat());
					currentRow.createCell(2).setCellValue(medicalAdmin.getName());
				}
				if(recording.getCallTime() != null){
					currentRow.createCell(3).setCellValue(recording.getCallTime());
				}
				if(recording.getTalkTime() != null){
					currentRow.createCell(4).setCellValue(recording.getTalkTime());
				}
				if(StringUtils.isNotBlank(recording.getIsOpen())){
					if("0".equals(recording.getIsOpen())){
						currentRow.createCell(5).setCellValue("不公开");
					}else if("1".equals(recording.getIsOpen())){
						currentRow.createCell(5).setCellValue("公开");
					}
				}
			}
		}
		return workbook;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Workbook exportGroupRecording(MedicalAdmin admin,MedicalCallRecording recording) throws Exception {
		List<Object> objectList = new ArrayList<Object>();
		StringBuilder statusSql = new StringBuilder("from MedicalCallRecording m where 1=1 and m.groupId=? and m.hospitalId=? ");
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql = new StringBuilder("from MedicalCallRecording m where 1=1 and m.groupId=? and m.hospitalId=? ");
			objectList.add(admin.getGroup().getId());
		}else{
			statusSql = new StringBuilder("from MedicalCallRecording m where m.hospitalId=? ");
		}
		objectList.add(admin.getEnterpriseId());
		
		if(StringUtils.isNotBlank(recording.getCallerNum())){
			statusSql.append("and m.callerNum like ? ");
			objectList.add("%"+recording.getCallerNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getCalledNum())){
			statusSql.append("and m.calledNum like ? ");
			objectList.add("%"+recording.getCalledNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getSeat())){
			statusSql.append("and m.seat = ? ");
			objectList.add(recording.getSeat());
		}
		if(recording.getStartTime() != null){
			statusSql.append("and m.callTime >= ? ");
			objectList.add(recording.getStartTime());
		}
		if(recording.getEndTime() != null){
			statusSql.append("and m.callTime <= ? ");
			objectList.add(recording.getEndTime());
		}
		if(recording.getTalkTimeLess() != null ){
			statusSql.append("and m.talkTime < ? ");
			objectList.add(recording.getTalkTimeLess());
		}
		if(recording.getTalkTimeMore() != null ){
			statusSql.append("and m.talkTime > ? ");
			objectList.add(recording.getTalkTimeMore());
		}
		// 获得记录
		List<MedicalCallRecording> list = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_RECORDING);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		
		Sheet sheet = workbook.getSheetAt(0);
		int size = list.size();
		for (int i = 0;i<size;i++) {
			Row currentRow = sheet.createRow(i+1);
			if(StringUtils.isNotBlank(list.get(i).getCallerNum())){
				currentRow.createCell(0).setCellValue(list.get(i).getCallerNum());
			}
			if(StringUtils.isNotBlank(list.get(i).getCalledNum())){
				currentRow.createCell(1).setCellValue(list.get(i).getCalledNum());
			}
			if(StringUtils.isNotBlank(list.get(i).getSeat())){
				MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, list.get(i).getSeat());
				if(null != medicalAdmin){
					currentRow.createCell(2).setCellValue(medicalAdmin.getName());
				}
				
			}
			if(list.get(i).getCallTime() != null){
				currentRow.createCell(3).setCellValue(list.get(i).getCallTime());
			}
			if(list.get(i).getTalkTime() != null){
				currentRow.createCell(4).setCellValue(list.get(i).getTalkTime());
			}
			if(StringUtils.isNotBlank(list.get(i).getIsOpen())){
				if("0".equals(list.get(i).getIsOpen())){
					currentRow.createCell(5).setCellValue("不公开");
				}else if("1".equals(list.get(i).getIsOpen())){
					currentRow.createCell(5).setCellValue("公开");
				}
			}
		}
		return workbook;
	}

	@Override
	public Workbook currentExportGroupRecording(MedicalAdmin admin, String ids) throws Exception {
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_RECORDING);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		if(StringUtils.isNotBlank(ids)){
			String [] ordersIds =  ids.split(",");
			for(int j=0;j<ordersIds.length;j++){
				// 获得记录
				MedicalCallRecording recording = (MedicalCallRecording)this.iBaseDao.getObject(MedicalCallRecording.class, ordersIds[j]);
				Row currentRow = sheet.createRow(j+1);
				if(StringUtils.isNotBlank(recording.getCallerNum())){
					currentRow.createCell(0).setCellValue(recording.getCallerNum());
				}
				if(StringUtils.isNotBlank(recording.getCalledNum())){
					currentRow.createCell(1).setCellValue(recording.getCalledNum());
				}
				if(StringUtils.isNotBlank(recording.getSeat())){
					MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, recording.getSeat());
					currentRow.createCell(2).setCellValue(medicalAdmin.getName());
				}
				if(recording.getCallTime() != null){
					currentRow.createCell(3).setCellValue(recording.getCallTime());
				}
				if(recording.getTalkTime() != null){
					currentRow.createCell(4).setCellValue(recording.getTalkTime());
				}
				if(StringUtils.isNotBlank(recording.getIsOpen())){
					if("0".equals(recording.getIsOpen())){
						currentRow.createCell(5).setCellValue("不公开");
					}else if("1".equals(recording.getIsOpen())){
						currentRow.createCell(5).setCellValue("公开");
					}
				}
			
			}
		}
		return workbook;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Workbook exportUserRecording(MedicalAdmin admin,MedicalCallRecording recording) throws Exception {
		List<Object> objectList = new ArrayList<Object>();
		StringBuilder statusSql = new StringBuilder("from MedicalCallRecording m where 1=1 and m.userId=? and m.hospitalId=? ");
		objectList.add(admin.getId());
		objectList.add(admin.getEnterpriseId());
		
		if(StringUtils.isNotBlank(recording.getCallerNum())){
			statusSql.append("and m.callerNum like ? ");
			objectList.add("%"+recording.getCallerNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getCalledNum())){
			statusSql.append("and m.calledNum like ? ");
			objectList.add("%"+recording.getCalledNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getSeat())){
			statusSql.append("and m.seat = ? ");
			objectList.add(recording.getSeat());
		}
		if(recording.getStartTime() != null){
			statusSql.append("and m.callTime >= ? ");
			objectList.add(recording.getStartTime());
		}
		if(recording.getEndTime() != null){
			statusSql.append("and m.callTime <= ? ");
			objectList.add(recording.getEndTime());
		}
		if(recording.getTalkTimeLess() != null ){
			statusSql.append("and m.talkTime < ? ");
			objectList.add(recording.getTalkTimeLess());
		}
		if(recording.getTalkTimeMore() != null ){
			statusSql.append("and m.talkTime > ? ");
			objectList.add(recording.getTalkTimeMore());
		}
		// 获得记录
		List<MedicalCallRecording> list = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_RECORDING);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		int size = list.size();
		for (int i = 0;i<size;i++) {
			Row currentRow = sheet.createRow(i+1);
			if(StringUtils.isNotBlank(list.get(i).getCallerNum())){
				currentRow.createCell(0).setCellValue(list.get(i).getCallerNum());
			}
			if(StringUtils.isNotBlank(list.get(i).getCalledNum())){
				currentRow.createCell(1).setCellValue(list.get(i).getCalledNum());
			}
			if(StringUtils.isNotBlank(list.get(i).getSeat())){
				MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, list.get(i).getSeat());
				currentRow.createCell(2).setCellValue(medicalAdmin.getName());
			}
			if(list.get(i).getCallTime() != null){
				currentRow.createCell(3).setCellValue(list.get(i).getCallTime());
			}
			if(list.get(i).getTalkTime() != null){
				currentRow.createCell(4).setCellValue(list.get(i).getTalkTime());
			}
			if(StringUtils.isNotBlank(list.get(i).getIsOpen())){
				if("0".equals(list.get(i).getIsOpen())){
					currentRow.createCell(5).setCellValue("不公开");
				}else if("1".equals(list.get(i).getIsOpen())){
					currentRow.createCell(5).setCellValue("公开");
				}
			}
		}
		return workbook;
	}

	@Override
	public Workbook currentExportUserRecording(MedicalAdmin admin, String ids) throws Exception {
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_RECORDING);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		if(StringUtils.isNotBlank(ids)){
			String [] ordersIds =  ids.split(",");
			for(int j=0;j<ordersIds.length;j++){
				// 获得记录
				MedicalCallRecording recording = (MedicalCallRecording)this.iBaseDao.getObject(MedicalCallRecording.class, ordersIds[j]);
				Row currentRow = sheet.createRow(j+1);
				if(StringUtils.isNotBlank(recording.getCallerNum())){
					currentRow.createCell(0).setCellValue(recording.getCallerNum());
				}
				if(StringUtils.isNotBlank(recording.getCalledNum())){
					currentRow.createCell(1).setCellValue(recording.getCalledNum());
				}
				if(StringUtils.isNotBlank(recording.getSeat())){
					MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, recording.getSeat());
					currentRow.createCell(2).setCellValue(medicalAdmin.getName());
				}
				if(recording.getCallTime() != null){
					currentRow.createCell(3).setCellValue(recording.getCallTime());
				}
				if(recording.getTalkTime() != null){
					currentRow.createCell(4).setCellValue(recording.getTalkTime());
				}
				if(StringUtils.isNotBlank(recording.getIsOpen())){
					if("0".equals(recording.getIsOpen())){
						currentRow.createCell(5).setCellValue("不公开");
					}else if("1".equals(recording.getIsOpen())){
						currentRow.createCell(5).setCellValue("公开");
					}
				}
			}
		}
		return workbook;
	}

	@Override
	public Page findOpenRecordingByPage(MedicalAdmin admin, MedicalCallRecording recording, Map<String, Object> map) {
		List<Object> objectList = new ArrayList<Object>();
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallRecording m where 1=1 and m.isOpen=? and m.hospitalId=? ");
		StringBuilder statusSql = new StringBuilder("from MedicalCallRecording m where 1=1 and m.isOpen=? and m.hospitalId=? ");
		objectList.add(SystemConfig.ISOPEN1);
		objectList.add(admin.getEnterpriseId());
		
		if(StringUtils.isNotBlank(recording.getCallerNum())){
			countSql.append("and m.callerNum like ? ");
			statusSql.append("and m.callerNum like ? ");
			objectList.add("%"+recording.getCallerNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getCalledNum())){
			countSql.append("and m.calledNum like ? ");
			statusSql.append("and m.calledNum like ? ");
			objectList.add("%"+recording.getCalledNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getSeat())){
			countSql.append("and m.seat = ? ");
			statusSql.append("and m.seat = ? ");
			objectList.add(recording.getSeat());
		}
		if(recording.getStartTime() != null){
			countSql.append("and m.callTime >= ? ");
			statusSql.append("and m.callTime >= ? ");
			objectList.add(recording.getStartTime());
		}
		if(recording.getEndTime() != null){
			countSql.append("and m.callTime <= ? ");
			statusSql.append("and m.callTime <= ? ");
			objectList.add(recording.getEndTime());
		}
		if(recording.getTalkTimeLess() != null ){
			countSql.append("and m.talkTime < ? ");
			statusSql.append("and m.talkTime < ? ");
			objectList.add(recording.getTalkTimeLess());
		}
		if(recording.getTalkTimeMore() != null ){
			countSql.append("and m.talkTime > ? ");
			statusSql.append("and m.talkTime > ? ");
			objectList.add(recording.getTalkTimeMore());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by m." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by m.callTime desc ");
		}
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalCallRecording> list = new ArrayList<MedicalCallRecording>();
		for(Object obj : page.getResult()) {
			MedicalCallRecording bean = (MedicalCallRecording) obj;
			if(StringUtils.isNotBlank(bean.getSeat())) {
				MedicalAdmin seat = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getSeat());
				if(null != seat){
					bean.setSeatName(seat.getName());
					list.add(bean);
				}
			}
		}
		page.setResult(list);
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Workbook exportOpenRecording(MedicalAdmin admin,MedicalCallRecording recording) throws Exception {
		List<Object> objectList = new ArrayList<Object>();
		StringBuilder statusSql = new StringBuilder("from MedicalCallRecording m where 1=1 and m.isOpen=? and m.hospitalId=? ");
		objectList.add(SystemConfig.ISOPEN1);
		objectList.add(admin.getEnterpriseId());
		
		if(StringUtils.isNotBlank(recording.getCallerNum())){
			statusSql.append("and m.callerNum like ? ");
			objectList.add("%"+recording.getCallerNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getCalledNum())){
			statusSql.append("and m.calledNum like ? ");
			objectList.add("%"+recording.getCalledNum()+"%");
		}
		if(StringUtils.isNotBlank(recording.getSeat())){
			statusSql.append("and m.seat = ? ");
			objectList.add(recording.getSeat());
		}
		if(recording.getStartTime() != null){
			statusSql.append("and m.callTime >= ? ");
			objectList.add(recording.getStartTime());
		}
		if(recording.getEndTime() != null){
			statusSql.append("and m.callTime <= ? ");
			objectList.add(recording.getEndTime());
		}
		if(recording.getTalkTimeLess() != null ){
			statusSql.append("and m.talkTime < ? ");
			objectList.add(recording.getTalkTimeLess());
		}
		if(recording.getTalkTimeMore() != null ){
			statusSql.append("and m.talkTime > ? ");
			objectList.add(recording.getTalkTimeMore());
		}
		// 获得记录
		List<MedicalCallRecording> list = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_RECORDING);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		int size = list.size();
		for (int i = 0;i<size;i++) {
			Row currentRow = sheet.createRow(i+1);
			
			if(StringUtils.isNotBlank(list.get(i).getCallerNum())){
				currentRow.createCell(0).setCellValue(list.get(i).getCallerNum());
			}
			if(StringUtils.isNotBlank(list.get(i).getCalledNum())){
				currentRow.createCell(1).setCellValue(list.get(i).getCalledNum());
			}
			if(StringUtils.isNotBlank(list.get(i).getSeat())){
				MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, list.get(i).getSeat());
				currentRow.createCell(2).setCellValue(medicalAdmin.getName());
			}
			if(list.get(i).getCallTime() != null){
				currentRow.createCell(3).setCellValue(list.get(i).getCallTime());
			}
			if(list.get(i).getTalkTime() != null){
				currentRow.createCell(4).setCellValue(list.get(i).getTalkTime());
			}
			if(StringUtils.isNotBlank(list.get(i).getIsOpen())){
				if("0".equals(list.get(i).getIsOpen())){
					currentRow.createCell(5).setCellValue("不公开");
				}else if("1".equals(list.get(i).getIsOpen())){
					currentRow.createCell(5).setCellValue("公开");
				}
			}
		}
		return workbook;
	}

	@Override
	public Workbook currentExportOpenRecording(MedicalAdmin admin, String ids) throws Exception {
		// Excel模版对象
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_RECORDING);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		if(StringUtils.isNotBlank(ids)){
			String [] ordersIds =  ids.split(",");
			for(int j=0;j<ordersIds.length;j++){
				// 获得记录
				MedicalCallRecording recording = (MedicalCallRecording)this.iBaseDao.getObject(MedicalCallRecording.class, ordersIds[j]);
				Row currentRow = sheet.createRow(j+1);
				
				if(StringUtils.isNotBlank(recording.getCallerNum())){
					currentRow.createCell(0).setCellValue(recording.getCallerNum());
				}
				if(StringUtils.isNotBlank(recording.getCalledNum())){
					currentRow.createCell(1).setCellValue(recording.getCalledNum());
				}
				if(StringUtils.isNotBlank(recording.getSeat())){
					MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, recording.getSeat());
					currentRow.createCell(2).setCellValue(medicalAdmin.getName());
				}
				if(recording.getCallTime() != null){
					currentRow.createCell(3).setCellValue(recording.getCallTime());
				}
				if(recording.getTalkTime() != null){
					currentRow.createCell(4).setCellValue(recording.getTalkTime());
				}
				if(StringUtils.isNotBlank(recording.getIsOpen())){
					if("0".equals(recording.getIsOpen())){
						currentRow.createCell(5).setCellValue("不公开");
					}else if("1".equals(recording.getIsOpen())){
						currentRow.createCell(5).setCellValue("公开");
					}
				}
			}
		}
		return workbook;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateRecordOpen(String ids) {
		String[] temp = ids.split("-");
		for (String s : temp) {
			if(StringUtils.isNotBlank(s)){
				this.iBaseDao.updateHql("update MedicalCallRecording a set a.isOpen='1' where a.id=? ", s);
			}
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateGroupRecordOpen(String ids) {
		String[] temp = ids.split("-");
		for (String s : temp) {
			if(StringUtils.isNotBlank(s)){
				this.iBaseDao.updateHql("update MedicalCallRecording a set a.isOpen='1' where a.id=? ", s);
			}
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateCenterRecordOpen(String ids) {
		String[] temp = ids.split("-");
		for (String s : temp) {
			if(StringUtils.isNotBlank(s)){
				this.iBaseDao.updateHql("update MedicalCallRecording a set a.isOpen='1' where a.id=? ", s);
			}
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateRecordCancel(String ids) {
		String[] temp = ids.split("-");
		for (String s : temp) {
			if(StringUtils.isNotBlank(s)){
				this.iBaseDao.updateHql("update MedicalCallRecording a set a.isOpen='0' where a.id=? ", s);
			}
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateCenterRecordCancel(String ids) {
		String[] temp = ids.split("-");
		for (String s : temp) {
			if(StringUtils.isNotBlank(s)){
				this.iBaseDao.updateHql("update MedicalCallRecording a set a.isOpen='0' where a.id=? ", s);
			}
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateGroupRecordCancel(String ids) {
		String[] temp = ids.split("-");
		for (String s : temp) {
			if(StringUtils.isNotBlank(s)){
				this.iBaseDao.updateHql("update MedicalCallRecording a set a.isOpen='0' where a.id=? ", s);
			}
		}
	}
}
